package com.hualan.sfzy.service.impl.consult;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.mapper.consult.ConsultGradeMapper;
import com.hualan.sfzy.mapper.consult.ConsultMapper;
import com.hualan.sfzy.model.entity.Consult;
import com.hualan.sfzy.model.entity.ConsultGrade;
import com.hualan.sfzy.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ConsultGradeServiceImpl implements BaseService<ConsultGrade> {
    @Autowired
    private ConsultGradeMapper mapper;
    @Autowired
    private ConsultMapper consultMapper;
    @Override
    public int add(ConsultGrade entity) {
        return mapper.insert( entity);
    }

    @Override
    public int edit(ConsultGrade entity) {
        return mapper.update(entity);
    }

    @Override
    public int remove(Integer id) {
        Consult consult = new Consult();
        consult.setConsultGrade(id);
        List<Consult> list = consultMapper.select(consult);
        // 判断该咨询师等级下是否有咨询师，如果有则不能删除
        if (list.size() > 0) {
            throw new RuntimeException("该咨询师等级下有咨询师，请先删除咨询师");
        }
        return mapper.delete(id);
    }

    @Override
    // 不能批量删除咨询师等级
    public int removeBatch(Integer[] ids) {
        return 0;
    }

    @Override
    public List<ConsultGrade> find() {
        return mapper.select(null);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, ConsultGrade entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<ConsultGrade> list = mapper.select(entity);
        PageInfo<ConsultGrade> info = new PageInfo<>(list);
        Map<String,Object>map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, info.getPages());
        map.put(CommonConstant.TOTAL, info.getTotal());
        return map;
    }

    @Override
    public ConsultGrade findById(Integer id) {
        return mapper.selectById(id);
    }
}
